Welcome to ChickR
It is assumed that the latest version of R is installed on your computer. It is recommended that you install and use Rstudio IDE (integrated development environment) for the ChickR series. Other IDEs should work fine. The first step is to install some packages (addons/apps) by typing the following in the console:
install.packages("tidyverse")
It could take some time to download and install. Without going into details, we next need to active some of these packages as we will be using them without directly realising. So please type the following in the console:
library(dplyr)
library(ggplot2)
The ChickWeight data is one of the many datasets included as part of R. The data are from a weight gain experiment for chicks. We will start by loading the data and looking at it.
data("ChickWeight")
glimpse(ChickWeight)
## Observations: 578
## Variables: 4
## $ weight <dbl> 42, 51, 59, 64, 76, 93, 106, 125, 149, 171, 199, 205, 4...
## $ Time <dbl> 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 0, 2, 4, 6, ...
## $ Chick <ord> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2...
## $ Diet <fctr> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
We can see that there are 4 variables and 578 observations. Each chick in the experiment is uniquely identified by the Chick variable (R is case sensitive) and they are randomly assigned to be fed one of four diets (Diet). Their weight (in grams) is measured over Time (from day zero to day 21).
Our objective is to investigate the effect of diet on the chieck weights over time.
It is good practice to understand your data before you do any fancy analysis. Start by looking the data graphically using the ggplot2 package. Don’t worry too much about the details of the commands just try to build your own understanding.
ggplot(ChickWeight, aes(Time, weight)) + geom_point()
We see that generally the chick weights increase over time but what is the effect of diet?
ggplot(ChickWeight, aes(Time, weight, colour = Diet)) + geom_point()
It is hard to make out the effect of the diet as there are many overlapping points.
ggplot(ChickWeight, aes(Time, weight, colour = Diet)) + geom_jitter()
This looks like four hives of bees spreading out so still not easy to see what the effect of diet.
ggplot(ChickWeight, aes(Time, weight)) + geom_point() + facet_wrap(~Diet)
Perhaps it will look better with a bit of colour
ggplot(ChickWeight, aes(Time, weight, colour = Diet)) + geom_point() + facet_wrap(~Diet)
Does it help to identify each chick?
ggplot(ChickWeight, aes(Time, weight, colour = Chick)) + geom_point() + facet_wrap(~Diet)
It sort of helps but there is still a lot of information to process. Let’s use lines instead of points.
ggplot(ChickWeight, aes(Time, weight)) + geom_line() + facet_wrap(~Diet)
Whoops not what we expected. We want one line for each chick. Let’s try again.
ggplot(ChickWeight, aes(Time, weight, colour = Chick)) + geom_line() + facet_wrap(~Diet)
Better but the legend is not strictly necessary. Trying again.
ggplot(ChickWeight, aes(Time, weight, group = Chick)) + geom_line() + facet_wrap(~Diet)
Oh… we’ve lost the colours.
ggplot(ChickWeight, aes(Time, weight, group = Chick, colour=Chick)) + geom_line() + facet_wrap(~Diet)
Perhaps the aesthetics (aes) need to be in the geom_line part.
ggplot(ChickWeight, aes(Time, weight)) + geom_line(aes(group = Chick)) + facet_wrap(~Diet)
Black and white again but no legend. Yay. Let’s upgrade to colour.
ggplot(ChickWeight, aes(Time, weight)) + geom_line(aes(colour = Chick)) + facet_wrap(~Diet)
What is going on??? Colour but the legend is back. Mixing colour and group in geom_line.
ggplot(ChickWeight, aes(Time, weight)) + geom_line(aes(colour = Chick, group = Chick)) + facet_wrap(~Diet)
ggplot(ChickWeight, aes(Time, weight)) + geom_line(aes(colour = Chick), show.legend = FALSE) + facet_wrap(~Diet)
ggplot(ChickWeight, aes(Time, weight)) + geom_line(aes(colour = Chick), show.legend = FALSE) + geom_point() + facet_wrap(~Diet)
ggplot(ChickWeight, aes(Time, weight)) + geom_line(aes(colour = Chick), show.legend = FALSE) + geom_point(aes(colour = Chick)) + facet_wrap(~Diet)
There are two problems here, the legend and the duplication of aes(colour = Chick). Let’s move it back to the aes in the ggplot part.
ggplot(ChickWeight, aes(Time, weight, colour = Chick)) + geom_line() + geom_point() + facet_wrap(~Diet)
Removing the legend again!
ggplot(ChickWeight, aes(Time, weight, colour = Chick)) + geom_line(show.legend = FALSE) + geom_point(show.legend = FALSE) + facet_wrap(~Diet)
What do they look like in one graph.
ggplot(ChickWeight, aes(Time, weight, colour = Chick)) + geom_line(show.legend = FALSE) + geom_point(show.legend = FALSE)
A spaghetti mess that does not tell us anything about the diet. Using Diet for colour instead of Chick.
ggplot(ChickWeight, aes(Time, weight, colour = Diet)) + geom_line(show.legend = FALSE) + geom_point(show.legend = FALSE)
Not what was hoped for. Bring back the Chick variable.
ggplot(ChickWeight, aes(Time, weight, colour = Diet, group=Chick)) + geom_line(show.legend = FALSE) + geom_point(show.legend = FALSE)
It makes sense to bring back the legend by removing both show.legend = FALSE
ggplot(ChickWeight, aes(Time, weight, colour = Diet, group=Chick)) + geom_line() + geom_point()
Move the legend to the bottom
ggplot(ChickWeight, aes(Time, weight, colour = Diet, group=Chick)) + geom_line() + geom_point() + theme(legend.position = "bottom")